
/**
 *
 * @param <E>
 * @author David Coronado
 */
public class CircularList<E> {

   private Node<E> head;
	private Node<E> tail;


        public CircularList()
	{
		head = tail = null;
	}
	
        /**
         *
         * @param o ; object to be addded at the head list
         */
        public void addFirst(E o)
	{
		if(head == null)
		{
			tail = head = new Node<E>(o, head);
		}
		else
		{
			head = new Node<E>(o, head);
			tail.next = head;
		}
	}

        /**
         *
         * @param o ; object to be added at the tail of the list
         */
        public void addLast(E o)
	{
		if(head == null)
		{
			addFirst(o);
		}
		else
		{
			tail.next = new Node<E>(o, head);
			tail = tail.next;
		}
	}

        /**
         *
         * @return the element removed from the head of the list
         */
        public E removeFirst()
	{
		if(head == null)
		{
			return null;
		}
		if(head == tail)
		{
			E tp = head.key;
			head = tail = null;
			return tp;
		}
		else
		{
			E tp = head.key;
			head = head.next;
			tail.next = head;
			return tp;
		}
	}

        /**
         *
         * @return a peek of the element in the head of the list
         */
        public E getHead()
	{
		if(head == null) return null;
		else return head.key;
	}
}
